import { createSlice } from '@reduxjs/toolkit';
import type { PayloadAction } from '@reduxjs/toolkit';

interface SearchState {
  searchValue: string;
  suggestions: {
    templates: string[];
    tools: string[];
    categories: string[];
  };
}

const initialState: SearchState = {
  searchValue: '',
  suggestions: {
    templates: [],
    tools: [],
    categories: []
  }
};

export const searchSlice = createSlice({
  name: 'search',
  initialState,
  reducers: {
    setSearchValue: (state, action: PayloadAction<string>) => {
      state.searchValue = action.payload;
    },
    setSuggestions: (state, action: PayloadAction<{
      templates: string[];
      tools: string[];
      categories: string[];
    }>) => {
      state.suggestions = action.payload;
    },
    clearSuggestions: (state) => {
      state.suggestions = {
        templates: [],
        tools: [],
        categories: []
      };
    }
  }
});

export const { setSearchValue, setSuggestions, clearSuggestions } = searchSlice.actions;
export default searchSlice.reducer;